#include "widget.h"
#include "ui_widget.h"
#include <QTreeWidgetItem>

Widget::Widget(QWidget *parent)
    : QWidget(parent)
    , ui(new Ui::Widget)
{
    ui->setupUi(this);

    ui->listWidget->addItem("first thing");
    ui->listWidget->addItem("second thing");
    ui->listWidget->addItem("thrid thing");

//    ui->tableWidget->setRowCount(2);
//    ui->tableWidget->setColumnCount(2);
//    ui->tableWidget->setItem(0,0, new QTableWidgetItem("first thing"));
//    ui->tableWidget->setItem(0,1, new QTableWidgetItem("second thing"));
//    ui->tableWidget->setItem(1,0, new QTableWidgetItem("third thing"));
//    ui->tableWidget->setItem(1,1, new QTableWidgetItem("forth thing"));

    //ui->treeWidget->insertTopLevelItem(0,new QTreeWidgetItem({"第一章"}));
//    ui->treeWidget->addTopLevelItem(new QTreeWidgetItem({"第一章"}));
//    ui->treeWidget->insertTopLevelItem(1,new QTreeWidgetItem({"第二章"}));
}

Widget::~Widget()
{
    delete ui;
}


void Widget::on_listWidget_itemClicked(QListWidgetItem *item)
{
    //qDebug() << item->text();
}

/*************************************ListWidget******************************************/
void Widget::on_listPushButton_clicked()
{
    ui->listWidget->addItem(ui->listLineEdit->text());
    ui->listLineEdit->setText("");
}

void Widget::on_listPushButton_2_clicked()
{
    QListWidgetItem* item = ui->listWidget->currentItem();
    //qDebug() << item->text();
    //ui->listWidget->removeItemWidget(item);
    ui->listWidget->takeItem(ui->listWidget->currentRow());
}


/*************************************TableWidget******************************************/
//新增行和列时，若无选中的，则默认在最后加；否则加在其之前
void Widget::on_rowAddPushButton_clicked()
{
    int row = ui->tableWidget->currentRow();
    //qDebug() << row;
    if(row == -1)
    {
        row = ui->tableWidget->rowCount();
    }
    ui->tableWidget->insertRow(ui->tableWidget->rowCount());
}

void Widget::on_rowDelPushButton_clicked()
{
    ui->tableWidget->removeRow(ui->tableWidget->currentRow());
}

void Widget::on_colAddPushButton_clicked()
{
    int column = ui->tableWidget->currentColumn();
    if(column == -1)
    {
        column = ui->tableWidget->columnCount();    //默认最后加，刚开始无数据也可以添加成功
    }

    ui->tableWidget->insertColumn(column);
    QString label = ui->tableLineEdit->text();
    ui->tableWidget->setHorizontalHeaderItem(column, new QTableWidgetItem(label));

    ui->tableWidget->selectColumn(column);      //选中新增的一列
}

void Widget::on_colDelPushButton_clicked()
{
    ui->tableWidget->removeColumn(ui->tableWidget->currentColumn());
}


/*************************************TreeWidget******************************************/
void Widget::on_topAddPushButton_clicked()
{
    //ui->treeWidget->addTopLevelItem(new QTreeWidgetItem({ui->treeLineEdit->text()}));
    //最好对输入框的内容进行判断，为空不插入
    QString text = ui->treeLineEdit->text();
    if(text == "")
    {
        return;
    }

    QTreeWidgetItem* item = new QTreeWidgetItem();
    item->setText(0, text);
    ui->treeWidget->addTopLevelItem(item);
}

void Widget::on_curAddPushButton_clicked()
{
    QString text = ui->treeLineEdit->text();
    if(text == "")
    {
        return;
    }

    QTreeWidgetItem* item = ui->treeWidget->currentItem();
    if(item == nullptr)
    {
        return;
    }

    //添加的是其子节点
    QTreeWidgetItem* childItem = new QTreeWidgetItem();
    childItem->setText(0,text);
    item->addChild(childItem);

    //添加完毕后直接将父节点展开
    item->setExpanded(true);

}

void Widget::on_curDelPushButton_clicked()
{
    QTreeWidgetItem* item = ui->treeWidget->currentItem();
    if(item == nullptr)
    {
        return;
    }

    QTreeWidgetItem* parentItem = item->parent();
    if(parentItem == nullptr)       //顶层节点
    {
        ui->treeWidget->takeTopLevelItem(ui->treeWidget->indexOfTopLevelItem(item));
    }
    else
    {
        parentItem->removeChild(item);
    }
}

